package learning.groovy.sql

import groovy.sql.Sql

class DBDemo {

	static main(args) {
		Sql sql =Sql.newInstance("jdbc:mysql://localhost:3306/test2??useUnicode=true&characterEncoding=utf-8","root","root","com.mysql.jdbc.Driver")
		println("执行查询语句")

		def currentTime=sql.firstRow ("select now() as currentTime ")
		println currentTime[0];

		def tenantryId="133f1815-588d-4858-868a-d6e2fcec2f6d"
		//tenantryId="error-133f1815-588d-4858-868a-d6e2fcec2f6d"
		def users=sql.query ("select id,username,email from web_user where tenantry_id=$tenantryId",{
			while(it.next()){
				println "id is "+it.getString("id");
				println "username is "+it.getString("username");
				println "email is "+it.getString("email");
			}
		})
		println "users:"+users;

		println("执行修改类语句");
		def id="93397cc9-88d5-4a0a-8b5a-6fffd8b05402"
		sql.execute ("insert  into `web_user`(`id`,`create_date`,`create_user`,`modify_date`,`modify_user`,`email`,`password`,`username`,`tenantry_id`) values ($id,'2012-06-27 08:25:25',NULL,'2012-06-27 08:25:25',NULL,'admin@admin.com','21232f297a57a5a743894a0e4a801fc3','admin','133f1815-588d-4858-868a-d6e2fcec2f6d');");

		sql.execute("update web_user set username = 'dddd' where id = ?",[id]);

		sql.eachRow("select * from web_user",{println it.id + " -- ${it.username} -- ${it.email} -- ${it.create_date}"})

		def row = sql.firstRow("select username,email from web_user");
		println "Row: username = ${row.username} and email = ${row.email}";

		sql.execute("delete from web_user where id = ?",[
			"93397cc9-88d5-4a0a-8b5a-6fffd8b05402"
		])

	}

}
